from pydantic import BaseModel
from typing import List, Optional
import datetime

# ------------------------------
# 3. Pydantic 响应模型（前端数据格式）
# ------------------------------

class UserBase(BaseModel):
    username: str
    email: str

class UserCreate(UserBase):
    password: str

class UserCreateAdmin(UserCreate):
    is_admin: bool = False  # 管理员创建用户时可指定是否为管理员

class User(UserBase):
    id: int
    is_active: bool
    is_admin: bool
    class Config:
        from_attributes = True

class ProductBase(BaseModel):
    name: str
    description: str
    price: float
    image_url: str
    stock: int
    category: str
    reviews: int
    rating: float
    is_new: Optional[bool] = False

class ProductCreate(ProductBase):
    pass

class Product(ProductBase):
    id: int
    
    class Config:
        from_attributes = True

class CartItemBase(BaseModel):
    product_id: int
    quantity: int = 1

class CartItemCreate(CartItemBase):
    pass

class CartItem(CartItemBase):
    id: int
    user_id: int
    product: Product
    
    class Config:
        from_attributes = True

class CartItemList(BaseModel):
    items: List[CartItemCreate]

class CartItemResponse(BaseModel):
    """购物车项响应模型"""
    id: int
    product_id: int
    quantity: int
    product: Product  # 包含完整商品信息
    
    class Config:
        from_attributes = True

class CartResponse(BaseModel):
    """购物车整体响应模型（包含items字段，匹配前端期望）"""
    items: List[CartItemResponse]

class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None